package com.bjqg.bjqg01.bjqg.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.ajguan.library.EasyRefreshLayout;
import com.ajguan.library.LoadModel;
import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.adapter.HomeAdapter;
import com.bjqg.bjqg01.bjqg.bean.HomeBean;
import com.bjqg.bjqg01.bjqg.utils.GlideImageLoader;
import com.bjqg.bjqg01.bjqg.view.LooperTextView;
import com.brioal.adtextviewlib.entity.AdEntity;
import com.brioal.adtextviewlib.view.ADTextView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/1/4.
 */

public class NewHomeActivity extends BaseActivity {

    private WindowManager windowManager;
    private Banner banner;
    private LooperTextView looperview;
    private RecyclerView rvHome;
    private View viewinflte;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_home);
        windowManager = this.getWindowManager();
        initView();
    }

    private void initView() {


        rvHome = (RecyclerView) findViewById(R.id.rv_home);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvHome.setLayoutManager(layoutManager);

        ArrayList<HomeBean> gethombean = gethombean();
        HomeAdapter homeAdapter = new HomeAdapter(gethombean);
        viewinflte = View.inflate(NewHomeActivity.this, R.layout.item_new_home_header, null);
        homeAdapter.addHeaderView(viewinflte);
        //轮播图
        initBanner();
        //滚动标题
//        looperviewShow();
        showADTextView();
        // pull-up loading
        pullUpLoading(homeAdapter);

        rvHome.setAdapter(homeAdapter);
    }

    private void pullUpLoading(final HomeAdapter homeAdapter) {
        final EasyRefreshLayout easyRefreshLayout = (EasyRefreshLayout) findViewById(R.id.refresh_home_layout);
        easyRefreshLayout.setLoadMoreModel(LoadModel.COMMON_MODEL);
        easyRefreshLayout.addEasyEvent(new EasyRefreshLayout.EasyEvent() {
            @Override
            public void onLoadMore() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        final List<String> list = new ArrayList<>();
                        for (int j = 0; j < 5; j++) {
                            list.add("this is  new load data >>>>" + new Date().toLocaleString());
                        }

                        //adapter.addData(list);

                        easyRefreshLayout.loadMoreComplete(new EasyRefreshLayout.Event() {
                            @Override
                            public void complete() {
//                                homeAdapter.getData().addAll(list);
//                                homeAdapter.notifyDataSetChanged();
                                Toast.makeText(NewHomeActivity.this,"没有更多数据",Toast.LENGTH_SHORT).show();
                            }
                        }, 500);

                    }
                }, 2000);


            }

            @Override
            public void onRefreshing() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        List<String> list = new ArrayList<>();
//                        for (int i = 0; i < 20; i++) {
//                            list.add("this is refresh data >>>" + new Date().toLocaleString());
//                        }
//                        adapter.setNewData(list);
//                        easyRefreshLayout.refreshComplete();
                        Toast.makeText(getApplicationContext(), "refresh success", Toast.LENGTH_SHORT).show();
                    }
                }, 1000);

            }
        });

    }

    private void showADTextView() {

        ADTextView mADTextView = (ADTextView) viewinflte.findViewById(R.id.ad_textview);
        ArrayList<AdEntity> mList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mList.add(new AdEntity("通知"+i, "内容"+i, "连接"+i));
        }

//        mList.add(new AdEntity("前缀2", "内容2", "连接2"));
//        mList.add(new AdEntity("前缀3", "内容3", "连接3"));
//        mList.add(new AdEntity("前缀4", "内容4", "连接4"));
        mADTextView.setFrontColor(Color.RED);
        mADTextView.setBackColor(Color.BLACK);
        mADTextView.setTexts(mList);
        mADTextView.setOnItemClickListener(new ADTextView.OnItemClickListener() {

            private Toast mToast;

            @Override
            public void onClick(String mUrl) {
                if (mToast == null) {
                    mToast = Toast.makeText(NewHomeActivity.this, mUrl, Toast.LENGTH_LONG);
                } else {
                    mToast.setText(mUrl);
                }
                mToast.show();
            }
        });
    }

    private void looperviewShow() {
        looperview = (LooperTextView) viewinflte.findViewById(R.id.looperview);
        //通知滚动
        looperview.setTipList(generateTips());
    }

    private void initBanner() {
        Integer[] images={R.mipmap.bgcx,R.mipmap.bgsb,R.mipmap.cxpj,
                R.mipmap.go_recharge_alipay,R.mipmap.go_recharge_wechat,
                R.mipmap.gonggao_img};
        ArrayList<Integer> imageViews = new ArrayList<>();
        for (int i = 0; i < images.length; i++) {
            imageViews.add(images[i]);
        }
        banner = (Banner) viewinflte.findViewById(R.id.banner);
//        banner = (Banner) findViewById(R.id.banner);
        ViewGroup.LayoutParams layoutParams = banner.getLayoutParams();
        layoutParams.height=getheigh()/4;
//        layoutParams.width=getwith()/2;
        banner.setLayoutParams(layoutParams);
        //设置图片集合
        banner.setImages(imageViews);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.RIGHT);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    public int getwith(){
        int width = windowManager.getDefaultDisplay().getWidth();
        return width;
    }

    public int getheigh(){
        int height = windowManager.getDefaultDisplay().getHeight();
        return height;
    }

    @Override
    protected void onStart() {
        super.onStart();
        //开始轮播
        banner.startAutoPlay();
    }

    @Override
    protected void onStop() {
        super.onStop();
        //结束轮播
        banner.stopAutoPlay();
//        looperview.stopNestedScroll();
    }

    private ArrayList<String> generateTips() {
        ArrayList<String> tips = new ArrayList<>();
        for (int i = 0; i < 50; i++) {

            tips.add("这有通知:"+i);
        }

        return tips;
    }

    private ArrayList<HomeBean> gethombean() {
        ArrayList<HomeBean> tips = new ArrayList<>();
        for (int i = 0; i < 50; i++) {

//            tips.add("公告列表"+i);
            tips.add(new HomeBean(i,i+"行业新闻"+i,i%2+1));
        }

        return tips;
    }
}
